package com.ceres.system.controller;

import com.ceres.entity.PageResult;
import com.ceres.entity.Result;
import com.ceres.entity.StatusCode;
import com.ceres.system.pojo.Admin;
import com.ceres.system.service.AdminService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    //private String name = SecurityContextHolder.getContext().getAuthentication().getName();
    private String name="gouderder";

    @PostMapping("/add")
    public Result add(Integer[] roleIds,@RequestBody Admin admin){
        try {
            adminService.addAdmin(roleIds,admin);
            return new Result(true,StatusCode.OK);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(true, StatusCode.ERROR);
        }
    }

    @GetMapping("/findAll")
    public Result findAll(){
        try {
            List<Admin> adminList = adminService.findAll();
            return new Result(true,StatusCode.OK,adminList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR);
        }
    }

    @DeleteMapping("/deleteById/{id}")
    public Result del(@PathVariable(name = "id") Integer id){
        try {
            adminService.delete(id);
            return new Result(true,StatusCode.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR);
        }
    }
    @PutMapping("/update")
    public Result update(@RequestBody Admin admin,Integer[] roleIds){
        try {
            adminService.updateAdmin(roleIds, admin);
            return new Result(true,StatusCode.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR);
        }
    }

    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable(name = "id")Integer id){
        try {
            Admin admin = adminService.findById(id);
            return new Result(true,StatusCode.OK,admin);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR);
        }
    }

    @GetMapping("/findRoleByAdmin/{id}")
    public Result findRoleByAdmin(@PathVariable(name = "id")Integer id){
        try {
            Integer[] roleByAdmin = adminService.findRoleByAdmin(id);
            return new Result(true,StatusCode.OK,roleByAdmin);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR);
        }
    }

    @PutMapping("/reset/{id}")
    public Result reset(@PathVariable(name = "id")Integer id){
        try {
            adminService.resetPassword(id);
            return new Result(true,StatusCode.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR);
        }
    }

    @GetMapping("/queryAdmin")
    public Result queryAdmin(@RequestParam Map queryCondition,@RequestParam(required = false,defaultValue = "1") int page,@RequestParam(required = false,defaultValue = "5") int size){
        try {
            Page<Admin> pageList = adminService.queryAdmin(queryCondition, page, size);
            PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
            return new Result(true,StatusCode.OK,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR);
        }
    }

    @PutMapping("/alterPassword")
    public Result alterPassword(String password) {
        try {
            adminService.changPassword(password, name);
            return new Result(true, StatusCode.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR);
        }
    }
}
